import { RouteRecordRaw } from 'vue-router';
import { _ } from '../util';
import AdminLayout from "../layout/admin/AdminLayout.vue";
import Home from '../views/Home.vue';
import Login from '../user/Login.vue';
import RenYuan from '../views/RenYuan.vue';
import Dept from '../views/Dept.vue';
import E404 from '../views/404.vue';
import E401 from '../views/401.vue';
const About = () => import('../views/About.vue');

import data from "./menu"; // 导航数据设定

// 管理端路同上，需要权限
const adminLayout: RouteRecordRaw[] = [];
adminLayout.push({ path: '', name: 'Home', component: Home, meta: { allow: true } });
adminLayout.push({ path: '/jiagou/dept', name: 'Dept', component: Dept, meta: { allow: false } });
adminLayout.push({ path: '/jiagou/renyuan', name: 'RenYuan', component: RenYuan, meta: { allow: false } });
adminLayout.push({ path: '/sss/about', name: 'About', component: About, meta: { allow: false } });
adminLayout.push({ path: '/error/401', name: 'A401', component: E401, meta: { allow: false } });

// 菜单限权匹配
let menu_obj: any = {};
data.map((o: any) => {
    menu_obj[o.name] = o;
})

adminLayout.map((o: any) => {
    if (menu_obj[o.name]) {
        o.meta.allow = true;
    }
})

// 管理端嵌套路由
const routers: RouteRecordRaw[] = [];
routers.push({ path: "/", name: "adminlaout", component: AdminLayout, children: adminLayout });


// 公用路由加载
routers.push({ path: "/login", name: "login", component: Login, meta: {} })
routers.push({ path: '/404', name: 'E404', component: E404, meta: { allow: true } });
routers.push({ path: '/401', name: 'E401', component: E401, meta: { allow: true } });
routers.push({ path: '/:catchAll(.*)', redirect: '/404' });

const menu = _.toTree(data, 'id', 'pid')

export { routers, menu };